package com.gzx.spring.apis.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gzx.risk.core.feign.request.ContractInfoReq;
import com.gzx.risk.core.feign.response.MiniFeignResp;
import com.gzx.risk.core.pojo.CreditFile;
import com.gzx.risk.core.service.OrderInfoService;
import com.gzx.spring.apis.client.MiniApiFeign;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    @Resource
    private MiniApiFeign miniApiFeign;

    private final ObjectMapper objectMapper;

    public OrderInfoServiceImpl(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override
    public CreditFile getContractInfo(String orderCode) throws Exception {
        ContractInfoReq infoReq = new ContractInfoReq();
        infoReq.setOrderCode(orderCode);
        infoReq.setContractIds(Arrays.asList(1L, 2L));

//        String requestStr = objectMapper.writeValueAsString(infoReq);
        String resp = miniApiFeign.queryContractAndUserExt(infoReq);

        MiniFeignResp result = null;
        if (resp != null && !resp.trim().isEmpty()) {
            result = objectMapper.readValue(resp, MiniFeignResp.class);
        } else {
            return null;
        }

        MiniFeignResp.ContractData data = result.getData();
        if (result.getCode() != 200 || Objects.isNull(data)) {
            log.error("Mini queryApplySign result error, orderCode : {}, resp: {}",orderCode, resp);
            return null;
        }

        // 授权书
        List<MiniFeignResp.Contract> contracts = data.getContracts();
        if (contracts == null || contracts.isEmpty() || contracts.size() < 2) {
            log.error("Mini queryApplySign contractList is null, orderCode : {}", orderCode);
            return null;
        }

        // 身份证信息
        MiniFeignResp.UserIdCard userIdCard = data.getUserIdCard();
        if (Objects.isNull(userIdCard)) {
            log.error("Mini queryApplySign userIdCard is null, orderCode : {}", orderCode);
            return null;
        }
        MiniFeignResp.IdCardSide faceIdCard = userIdCard.getFace();
        MiniFeignResp.IdCardSide backIdCard = userIdCard.getBack();
        if (Objects.isNull(faceIdCard) || Objects.isNull(backIdCard) || StringUtils.isBlank(faceIdCard.getImage())
                || StringUtils.isBlank(backIdCard.getImage())) {
            log.error("Mini queryApplySign face or back is null, orderCode : {}", orderCode);
            return null;
        }

        // 人脸识别信息
        MiniFeignResp.FaceRecognize face = data.getFace();
        if (Objects.isNull(face) || StringUtils.isBlank(face.getFaceImage())) {
            log.error("Mini queryApplySign face is null, orderCode : {}", orderCode);
            return null;
        }

        String caAuthUrl = ""; // ca授权书
        String guaranteeApplyUrl = ""; // 委托担保申请书
        for (MiniFeignResp.Contract contract : contracts) {
            if (contract.getContractId()==1L){
                guaranteeApplyUrl = contract.getFullUrl();
            }
            if (contract.getContractId()==2L){
                caAuthUrl = contract.getFullUrl();
            }
        }
        if (StringUtils.isBlank(caAuthUrl)||StringUtils.isBlank(guaranteeApplyUrl)) {
            log.error("Mini queryApplySign contract url is null, orderCode : {}", orderCode);
            return null;
        }

        CreditFile creditFile = new CreditFile();
        creditFile.setOrderCode(orderCode);
        creditFile.setIdCardFrontUrl(faceIdCard.getImage());
        creditFile.setIdCardBackUrl(backIdCard.getImage());
        creditFile.setPersonVerifyUrl(face.getFaceImage());
        creditFile.setCaAuthUrl(caAuthUrl);
        creditFile.setGuaranteeApplyUrl(guaranteeApplyUrl);
        return creditFile;
    }
}
